package com.yjd.api.user;

import com.yjd.comm.auth.dto.LivingBodyConfig;
import com.yjd.comm.auth.model.LivingBodyAuthModel;
import com.yjd.comm.auth.service.ILivingBodyAuthService;
import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.bank.service.IBankService;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.fundservice.enums.BusTypeEnum;
import com.yjd.comm.fundservice.util.QianKangServerUtil;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.util.DFormat;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.Constant;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 活体认证
 * Created by yjd-wap on 2018/2/8.
 */
@Controller
@Scope("prototype")
@RequestMapping("/livingBody/")
public class LivingBodyAuthControl extends BaseControl {


    @RequestMapping("auth")
    public void auth(@RequestParam(required = true) Integer bus_type) throws Exception {
        if(bus_type==null){
            FrameUtil.throwBaseException1("请求参不能为空");
        }
        Long user_id = getSessionUserId();
        Long currtime = FrameUtil.getTime();
        BankModel bankModel = bankService.getModelById(user_id, DbWREnums.WRITE);
        if(bankModel==null){
            FrameUtil.throwBaseException1("请先进行实名认证银行卡！");
        }
        //生成一个订单活体认证订单
        LivingBodyAuthModel authModel = new LivingBodyAuthModel(user_id,bus_type,currtime,currtime);
        authModel = livingBodyAuthService.insert(authModel);

        String livingConfig = SysParamUtil.getValue(Constant.SysParamKeyEnum.LIVING_AUTH_CALL_BACK_CONFIG);
        //获取活动认证配置
        LivingBodyConfig livingBodyConfig = StringUtil.getObject(livingConfig,LivingBodyConfig.class);
        String success_url =  DFormat.stringFormat(livingBodyConfig.getSucees_url(),FrameUtil.newHashMap("bus_type",bus_type,"user_id",user_id,"id",authModel.getId()));
        String fail_url =  DFormat.stringFormat(livingBodyConfig.getFail_url(),FrameUtil.newHashMap("bus_type",bus_type,"user_id",user_id,"id",authModel.getId()));

        //调用活体认证接口
       Map map = FrameUtil.newHashMap("name",bankModel.getId_name(),"identity",bankModel.getId_number(),"redirect_uri_suc",success_url,"redirect_uri_fail",fail_url);
        Map rsMap =  QianKangServerUtil.sendMsg(map, BusTypeEnum.LIVING_IDENTIFY,authModel.getId().toString(),Map.class);
       if(rsMap.isEmpty() || rsMap.get("living_identify_url")==null){
           FrameUtil.throwBaseException1("认证识别服务失败，请重试！");
       }
        successData(FrameUtil.newHashMap("jump_url",rsMap.get("living_identify_url"),"auth_id",authModel.getId()));
    }

    @Resource
    private IBankService bankService;

    @Resource
    private ILivingBodyAuthService livingBodyAuthService;
}
